// views/index/shop/translationcopy/ordermessage/index.js
var util = require('../../../../../../utils/util');
var BASE_URL = getApp().globalData.BASE_URL
import Notify from '../../../../../../miniprogram_npm/@vant/weapp/notify/notify';
import Dialog from '../../../../../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minDate: new Date(1970, 0, 1).getTime(),
    value: '',
    openjiudupop: false,
    showdatepop: false,
    currentDate: new Date().getTime(),
    nowtime: "",
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    liu_reason: '',
    xuan_reason: '',
    achievement: '',
    programme: '',
    skill: '',
    condition: '',
    planning: '',
    use: "申请签证",
    apply: '商科',
    language: '德语',
    beizhu: '',
    email: "",
    ifjiaji: false,
    price: 499,
    dongji_gao: ''
  },

  //确定时间
  change_date: function () {
    this.setData({
      showdatepop: false,
      born: this.changeTime(this.data.nowtime)
    })
  },
  change_jiudu: function () {
    this.setData({
      openjiudupop: false,
      du_time: this.changeTime(this.data.nowtime)
    })
  },
  //切换时间
  getColumnValue(event) {
    console.log(event)
    this.setData({
      nowtime: event.detail,
    });
  },

  //打开时间pop
  opendatepop: function () {
    this.setData({
      showdatepop: true
    })
  },
  openjiudupop: function () {
    this.setData({
      openjiudupop: true
    })
  },
  //关闭时间pop
  onClose: function () {
    this.setData({
      showdatepop: false
    })
  },
  //用途切换
  change_use: function (e) {
    this.setData({
      use: e.currentTarget.dataset.use
    })
  },
  //领域切换
  change_apply: function (e) {
    this.setData({
      apply: e.currentTarget.dataset.apply
    })
  },
  //语言切换
  change_language: function (e) {
    this.setData({
      language: e.currentTarget.dataset.language
    })
    if (e.currentTarget.dataset.language == '德语') {
      if (this.data.ifjiaji) {
        this.setData({
          price: 499 * 1.3
        })
      } else {
        this.setData({
          price: 499
        })
      }
    } else {
      if (this.data.ifjiaji) {
        this.setData({
          price: 399 * 1.3
        })
      } else {
        this.setData({
          price: 399
        })
      }
    }
  },
  //男女切换
  change_sex: function (e) {
    this.setData({
      sex: e.currentTarget.dataset.sex
    })
  },
  //是否使用加急服务
  ifjiaji: function () {
    this.setData({
      ifjiaji: !this.data.ifjiaji
    })
    if (this.data.ifjiaji) {
      this.setData({
        price: this.data.price * 1.3
      })
    } else {
      this.setData({
        price: this.data.price / 1.3
      })
    }
  },
  //上传文件
  upfile: function () {
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    wx.chooseMessageFile({
      count: 100,
      success(res) {
        wx.showLoading({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
        })
        res.tempFiles.forEach(item => {
          item.size = Math.floor(item.size / 1024 * 100) / 100
          console.log(item)
        })
        const tempFilePaths = res.tempFiles
        console.log(res, tempFilePaths)
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: `${BASE_URL}/fanyi/dongjiupload`, //仅为示例，非真实的接口地址
            filePath: tempFilePaths[i].path,
            name: 'file',
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              uid: userinfo.id,
              file: tempFilePaths[i].name
            },
            success(res) {
              _this.getfile()
              if (res.statusCode == 200) {
                wx.hideLoading()
                wx.showToast({
                  title: "上传成功",
                  icon: "none",
                  duration: 1500
                })
              }
              var data = JSON.parse(res.data)
              console.log(data)
              if (data.status == 4) {
                wx.showToast({
                  title: `上传失败,${data.error}`,
                  icon: "none",
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  },
  //删除文件
  del_file: function (e) {
    var userinfo = wx.getStorageSync("USER")
    var _this = this
    wx.showModal({
      title: '提示',
      content: '你确定要删除吗？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: `${BASE_URL}/fanyi/infodelete`,
            method: "GET",
            data: {
              id: e.currentTarget.dataset.id,
              type: 'xiaochengxv'
            },
            header: {
              'Content-Type': 'application/json' //GET方式是这个
            },
            success(res) {
              console.log(res)
              _this.getfile()
            }
          })
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },




  //立即办理
  placeorder: function () {
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    if (_this.data.liu_reason == '') {
      Notify('内容不能为空');
    }
    if (_this.data.xuan_reason == '') {
      Notify('内容不能为空');
    }
    if (_this.data.achievement == '') {
      Notify('内容不能为空');
    }
    if (_this.data.programme == '') {
      Notify('内容不能为空');
    }
    if (_this.data.skill == '') {
      Notify('内容不能为空');
    }
    if (_this.data.condition == '') {
      Notify('内容不能为空');
    }
    if (_this.data.planning == '') {
      Notify('内容不能为空');
    }
    if (!_this.data.liu_reason == '' && !_this.data.xuan_reason == '' &&! _this.data.achievement == '' && ! _this.data.programme == '' && ! _this.data.skill == '' && !_this.data.condition == '' && !_this.data.planning=='') {
      wx.request({
        url: `${BASE_URL}/fanyi/dongjixinnext`,
        method: "POST",
        data: {
          liu_reason: _this.data.liu_reason,
          xuan_reason: _this.data.xuan_reason,
          achievement: _this.data.achievement,
          programme: _this.data.programme,
          skill: _this.data.skill,
          condition: _this.data.condition,
          planning: _this.data.planning,
          use: _this.data.use,
          shenqing: _this.data.apply,
          language: _this.data.language,
          beizhu: _this.data.beizhu,
          jiaji: _this.data.beizhu ? 1 : 0,
          email: _this.data.email,
          uid: userinfo.id,
          type: 'xiaochengxv'
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          wx.navigateTo({
            url: `../dongjiorderdetail/index?id=${res.data.id}`
          })
        }
      })

    }
  },















  getfile: function () {
    var userinfo = wx.getStorageSync("USER")
    var _this = this
    wx.request({
      url: `${BASE_URL}/fanyi/dongjixin`,
      method: "GET",
      data: {
        uid: userinfo.id,
        type: 'xiaochengxv'
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        _this.setData({
          dongji_gao: res.data.dongji_gao,
          use: res.data.list.use,
          language: res.data.list.language,
          apply: res.data.list.shenqing,
          beizhu: res.data.list.beizhu == '' ? '无备注' : res.data.list.beizhu,
          email:  res.data.list.email,
          ifjiaji: res.data.list.jiaji == 1 ? true : false,
          price: res.data.list.amount
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getfile()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var userinfo = wx.getStorageSync("USER")
    this.setData({
      email: userinfo.email
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //手机号正则
  codeVerification: function (phone) {
    let phoneCodeVerification = /^[1][3,4,5,7,8][0-9]{9}$/;
    return phoneCodeVerification.test(phone);
  },
  //英文字母正则
  ifenglish: function (pinyin) {
    let ifenglish = /^[A-Za-z]+$/;
    return ifenglish.test(pinyin);
  },
  //汉字正则
  ifhanzi: function (pinyin) {
    let ifhanzi = /^[\u4e00-\u9fa5]+$/;
    return ifhanzi.test(pinyin);
  },
  //微信号正则
  ifwewhat: function (pinyin) {
    let ifwewhat = /^[0-9a-zA-Z]*$/g;
    return ifwewhat.test(pinyin);
  },
  //邮箱验证
  ifemail: function (email) {
    let ifemail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    return ifemail.test(email);
  },









  // 时间戳转换
  changeTime(time) {
    if (time) {
      var oDate = new Date(time * 1),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oTime = oYear + "/" + this.getBz(oMonth) + "/" + this.getBz(oDay)
      return oTime;
    } else {
      return "";
    }
  },
  // 补0
  getBz(num) {
    if (parseInt(num) < 10) {
      num = "0" + num;
    }
    return num;
  },


})